﻿; Crispy Plotter, a fast and easy-to-use function plotter
; Copyright (C) 2011 Konrad Kölzer

; Crispy Plotter is free software, see License.txt for details.

; Crispy Plotter Installer & Uninstaller
; Written by Konrad Kölzer
;

SetCompressor /SOLID /FINAL lzma

; --------------------------------
; Include Modern UI

	!include "MUI.nsh"
	!include TextFuncUTF16.nsh
	!include "WordFunc.nsh"
	!insertmacro WordReplace
	

; --------------------------------
; General

	; Version, name and file
	!define VERSION "1.8"
	Name "Crispy Plotter ${VERSION}"
	OutFile "Crispy_Plotter_${VERSION}.exe"

	; Default installation folder
	InstallDir "$PROGRAMFILES\Crispy Plotter"

	; Get installation folder from registry if available
	InstallDirRegKey HKCU "Software\Crispy Plotter" ""

	!define DirAppData "$APPDATA\Crispy Plotter"
	!define AssocType "CrispyPlotter.Function"
	!define AssocExt ".cpf"
	

; --------------------------------
; Interface Settings
	!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\orange-install.ico"
	!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\orange-uninstall.ico"
	!define MUI_COMPONENTSPAGE_SMALLDESC
	!define MUI_HEADERIMAGE
	!define MUI_HEADERIMAGE_BITMAP ".\res\Header.bmp"
	!define MUI_HEADERIMAGE_UNBITMAP ".\res\Header.bmp"
	!define MUI_WELCOMEFINISHPAGE_BITMAP ".\res\Side.bmp"
	!define MUI_UNWELCOMEFINISHPAGE_BITMAP ".\res\Side.bmp"
	!define MUI_ABORTWARNING

; --------------------------------
; Language Selection Dialog Settings

	; Remember the installer language
	!define MUI_LANGDLL_REGISTRY_ROOT "HKCU"
	!define MUI_LANGDLL_REGISTRY_KEY "Software\Crispy Plotter"
	!define MUI_LANGDLL_REGISTRY_VALUENAME "Installer Language"

; User variables
Var WorkspaceFolder ; Path of workspace folder

; --------------------------------
; Pages
	!define MUI_WELCOMEFINISHPAGE_CUSTOMFUNCTION_INIT InitInstaller

	
	!insertmacro MUI_PAGE_WELCOME
	!insertmacro MUI_PAGE_LICENSE "..\License.txt"
	!insertmacro MUI_PAGE_COMPONENTS
	!insertmacro MUI_PAGE_DIRECTORY

	; Add a directory page to let the user specify a workspace folder
	; Store the folder in $WorkspaceFolder
	!define MUI_DIRECTORYPAGE_VARIABLE $WorkspaceFolder
	!define MUI_DIRECTORYPAGE_TEXT_TOP $(LSWorkspaceTextTop)
	!define MUI_DIRECTORYPAGE_TEXT_DESTINATION $(LSWorkspaceDestination)
	!define MUI_PAGE_HEADER_TEXT $(LSWorkspaceHeaderText)
	!define MUI_PAGE_HEADER_SUBTEXT $(LSWorkspaceHeaderSubText)
	!insertmacro MUI_PAGE_DIRECTORY
	!insertmacro MUI_PAGE_INSTFILES
	!insertmacro MUI_PAGE_FINISH

	!insertmacro MUI_UNPAGE_CONFIRM
	!insertmacro MUI_UNPAGE_INSTFILES


; --------------------------------
; Languages

!include "CrispyPlotterLanguages.nsh"

; --------------------------------
; Reserve Files

	; If you are using solid compression, files that are required before
	; the actual installation should be stored first in the data block,
	; because this will make your installer start faster.

	!insertmacro MUI_RESERVEFILE_LANGDLL


; --------------------------------
; Installer Sections

Section !$(LSSecMain) SecMain
	SectionIn RO
	; Copy program files
	SetOutPath "$INSTDIR"
	File /r /x ".svn" /x "*.po" "..\bin\Locale"
	File /r /x ".svn" "..\bin\Help"
	File /r /x ".svn" "..\bin\Plugins"
	File "..\bin\CrispyPlotter.exe"
	File "..\License.txt"
	File "..\Contributors.txt"
	File "..\ChangeLog.txt"
	File "..\ReleaseNotes.txt"
	SetOutPath "$INSTDIR\Data"
	File "..\bin\Data\Document.ico"

	; Create workspace folder
	CreateDirectory $WorkspaceFolder
	; Create directory for application data:
	SetOutPath "${DirAppData}"
	
	; Create Settings.ini:
	StrCpy $1 "${DirAppData}\Settings.ini"
	FileOpen $0 $1 w
	FileWriteWord $0 0xFEFF ; Write the BOM
	; Write workspace folder to Settings.ini:
	StrCpy $R0 $WorkspaceFolder
    ; Escape back slashes in path string:
	${WordReplace} $R0 "\" "\\" "+" $R0
	StrCpy $R0 "Workspace=$R0$\r$\n"
    FileWriteUTF16LE $0 $R0
    ; Write current language to Settings.ini
	StrCpy $R0 "Language=$(LSLanguageCode)$\r$\n"
	FileWriteUTF16LE $0 $R0
	FileClose $0
	
	; Convert Settings.ini from UCS-2 LE to UTF-8:
    StrCpy $2 "ToUTF8"
	${FileRecode} $1 $2

	; Store installation folder
	WriteRegStr HKCU "Software\Crispy Plotter" "" $INSTDIR
	; Create uninstaller
	WriteUninstaller "$INSTDIR\Uninstall.exe"
SectionEnd

Section $(LSSecInstallExamples) SecInstallExamples
	SetOutPath $WorkspaceFolder
	File /r /x ".svn" /x "_Internal" "..\bin\Workspace\*.cpf"
SectionEnd

Section $(LSSecAssocFileType) SecAssocFileType
	; Back up old value of .cpf
	!define Index "Line${__LINE__}"
	ReadRegStr $1 HKCR "${AssocExt}" ""
	StrCmp $1 "" "${Index}-NoBackup"
	StrCmp $1 "${AssocType}" "${Index}-NoBackup"
	WriteRegStr HKCR "${AssocExt}" "backup_val" $1
	"${Index}-NoBackup:"
	; Save in registry , if file association has been set:
	WriteRegStr HKCU "Software\Crispy Plotter" "AssocFileType" "true"
	WriteRegStr HKCR "${AssocExt}" "" "${AssocType}"
	ReadRegStr $0 HKCR "${AssocType}" ""
	StrCmp $0 "" 0 "${Index}-Skip"
	WriteRegStr HKCR "${AssocType}" "" "$(LSDocType)"
	WriteRegStr HKCR "${AssocType}\shell" "" "open"
	WriteRegStr HKCR "${AssocType}\DefaultIcon" "" "$INSTDIR\Data\Document.ico,0"
	"${Index}-Skip:"
	WriteRegStr HKCR "${AssocType}\shell\open\command" "" \
	'"$INSTDIR\CrispyPlotter.exe" "%1"'
	System::Call 'Shell32::SHChangeNotify(i 0x8000000, i 0, i 0, i 0)'
	!undef Index
SectionEnd

Section $(LSSecCreateSMFolder) SecCreateSMFolder
	WriteRegStr HKCU "Software\Crispy Plotter" "CreateShortcuts" "true"
	SetShellVarContext All
	CreateDirectory "$SMPROGRAMS\Crispy Plotter"
	CreateShortCut "$SMPROGRAMS\Crispy Plotter\$(LSAppShortcut).lnk" "$INSTDIR\CrispyPlotter.exe"
	CreateShortCut "$SMPROGRAMS\Crispy Plotter\$(LSUninstallShortcut).lnk" "$INSTDIR\Uninstall.exe"
	CreateShortCut "$SMPROGRAMS\Crispy Plotter\$(LSManualShortcut).lnk" "$INSTDIR\Help\CrispyPlotter_$(LSManualLanguage).chm"
SectionEnd

; --------------------------------
; Installer Functions

Function InitInstaller
	StrCpy $WorkspaceFolder "$DOCUMENTS\$(LSMyFunctions)"
FunctionEnd

Function .onInit
	!insertmacro MUI_LANGDLL_DISPLAY
FunctionEnd



; --------------------------------
; Descriptions

	; Assign language strings to sections
	!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
	!insertmacro MUI_DESCRIPTION_TEXT ${SecMain} $(LSSecMainDesc)
	!insertmacro MUI_DESCRIPTION_TEXT ${SecInstallExamples} $(LSSecInstallExamplesDesc)
	!insertmacro MUI_DESCRIPTION_TEXT ${SecAssocFileType} $(LSSecAssocFileTypeDesc)
	!insertmacro MUI_DESCRIPTION_TEXT ${SecCreateSMFolder} $(LSSecCreateSMFolder)
	!insertmacro MUI_FUNCTION_DESCRIPTION_END

; --------------------------------
; Uninstaller Section

Section "Uninstall"
	; Look in registry , if file association has been set by installer:
	ReadRegStr $1 HKCU "Software\Crispy Plotter" "AssocFileType"
	StrCmp $1 "true" 0 skipRemoveFileAssoc
	;start of restore script
	!define Index "Line${__LINE__}"
	ReadRegStr $1 HKCR "${AssocExt}" ""
	StrCmp $1 "${AssocType}" 0 "${Index}-NoOwn" ; only do this if we own it
	ReadRegStr $1 HKCR "${AssocExt}" "backup_val"
	StrCmp $1 "" 0 "${Index}-Restore" ; if backup="" then delete the whole key
	DeleteRegKey HKCR "${AssocExt}"
	Goto "${Index}-NoOwn"
	"${Index}-Restore:"
	WriteRegStr HKCR "${AssocExt}" "" $1
	DeleteRegValue HKCR "${AssocExt}" "backup_val"

	"${Index}-NoOwn:"
	DeleteRegKey HKCR "${AssocType}" ;Delete key with association settings
	System::Call 'Shell32::SHChangeNotify(i 0x8000000, i 0, i 0, i 0)'

	!undef Index
	;rest of script
	skipRemoveFileAssoc:

	; Remove shortcuts
	ReadRegStr $1 HKCU "Software\Crispy Plotter" "CreateShortcuts"
	StrCmp $1 "true" 0 skipRemoveShortcuts
	SetShellVarContext all
	Delete "$SMPROGRAMS\Crispy Plotter\$(LSAppShortcut).lnk"
	Delete "$SMPROGRAMS\Crispy Plotter\$(LSUninstallShortcut).lnk"
	Delete "$SMPROGRAMS\Crispy Plotter\$(LSManualShortcut).lnk"
	RMDir "$SMPROGRAMS\Crispy Plotter"
	skipRemoveShortcuts:

	; Remove files
	Delete "$INSTDIR\Uninstall.exe"
	Delete "$INSTDIR\Locale\*\LC_MESSAGES\default.mo"
	RMDir "$INSTDIR\Locale"

	Delete "$INSTDIR\Help\CrispyPlotter_*.chm"
	RMDir "$INSTDIR\Help"

	Delete "$INSTDIR\Plugins\WaveOut\WaveOut.dll"
    RMDir "$INSTDIR\Plugins\WaveOut\"
	Delete "$INSTDIR\Plugins\FileDump\FileDump.dll"
    RMDir "$INSTDIR\Plugins\FileDump\"
	RMDir "$INSTDIR\Plugins"

	Delete "$INSTDIR\Data\Document.ico"
	RMDir "$INSTDIR\Data"
	
	; Switch to current user to clean the application folder
	SetShellVarContext current
	Delete "${DirAppData}\Settings.ini"
	Delete "${DirAppData}\Forms.ini"
	RMDir "${DirAppData}"
	

	Delete "$INSTDIR\CrispyPlotter.exe"
	Delete "$INSTDIR\License.txt"
	Delete "$INSTDIR\Contributors.txt"
	Delete "$INSTDIR\ChangeLog.txt"
	Delete "$INSTDIR\ReleaseNotes.txt"
	RMDir "$INSTDIR"

	DeleteRegKey /ifempty HKCU "Software\Crispy Plotter"
SectionEnd

; --------------------------------
; Uninstaller Functions

Function un.onInit

	!insertmacro MUI_UNGETLANGUAGE

FunctionEnd